﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace CentralFoveaLab
{

    public static class Extensions
    {
        #region IList
        public static T Find<T>(this IList<T> list, Func<T, int, bool> predicate)
        {
            for (var i = 0; i < list.Count; i++)
            {
                var each = list[i];
                if (predicate(each, i))
                {
                    return each;
                }
            }
            return default(T);
        }
        #endregion


        #region IList<VectorUI>
        public static string ToLink(this IList<VectorUI> list, string rawPageUrl)
        {
            if (String.IsNullOrEmpty(rawPageUrl))
            {
                throw new Exception("IList<VectorUI>.ToLink(string) cannot be null or empty");
            }
            var qMarkIndex = rawPageUrl.IndexOf('?');
            var pageUrl = (qMarkIndex <= 0) ? rawPageUrl : rawPageUrl.Substring(0, qMarkIndex);
            var sb = new StringBuilder(pageUrl);
            if (list.Count > 0)
            {
                sb.Append("?vectors=");
            }
            var isFirst = true;
            foreach (var v in list)
            {
                if (isFirst)
                {
                    isFirst = false;
                }
                else
                {
                    sb.Append("v");
                }
                sb.AppendFormat("{0}/{1}/{2}/{3}/{4}/{5}",
                                v.BeginningX, v.BeginningY, v.BeginningZ,
                                v.EndX, v.EndY, v.EndZ);
            }
            return sb.ToString();
        }
        #endregion
    }
}
